package com.spring.schedule_system.dengxiaojiao.service;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.schedule_system.dengxiaojiao.repositories.VenDbDeliverySetRepositories;
import com.spring.schedule_system.dengxiaojiao.request.DeliverySetDomainIn;
import com.spring.schedule_system.dengxiaojiao.request.DeliverySetDomainOut;
import com.spring.schedule_system.dengxiaojiao.request.DeliverySetSaveDomainIn;
import com.spring.schedule_system.dengxiaojiao.request.VenDbDeliverySet;
import com.spring.schedule_system.dengxiaojiao.util.BizException;
import com.spring.schedule_system.dengxiaojiao.util.BusinessContext;
import com.spring.schedule_system.dengxiaojiao.util.ListResult;
import com.spring.schedule_system.dengxiaojiao.util.LyOptResultUtil;
import com.spring.schedule_system.dengxiaojiao.util.OptResult;
import com.spring.schedule_system.dengxiaojiao.util.StringUtil;
import com.szlanyou.cloud.uid.utils.LyIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/***********************************************************************************************************************
* <p>
    * 预交车天数设置 业务逻辑细分biz
    * </p>
* @author        ly-dengxiaojiao
* @date          2023-03-26
* @version       V1.0
**********************************************************************************************************************/
@Component
public class DeliverySetService {

    @Autowired
    VenDbDeliverySetRepositories venDbDeliverySetRepositories;


    public OptResult venDbCarDeliveryDaySetSave(DeliverySetSaveDomainIn deliverySetSaveDomainIn){
//        OptResult optResult = venDbDeliverySetRepositories.venDbCarDeliveryDaySetSave(deliverySetSaveDomainIn);
        String resutl = "操作失败";
        List<VenDbDeliverySet> saveDatas = deliverySetSaveDomainIn.getSaveDatas();
        /**
         * 若modifyType为batchSame时的批量同步，直接循环saveDatas中的数据，在循环过程中，
         * 先以专营店id查询表【t_oms_ven_db_delivery_set】中是否存在数据，
         * 若存在则更新当前数据；若不存在数据则新增数据
         */
        if ("batchSame".equals(deliverySetSaveDomainIn.getModifyType())) {
            if (StringUtil.IsEmptyOrNull(saveDatas) || saveDatas.size() <= 0) {
                throw BizException.newBizException("500", "新增操作数据为空");
            }
            List<VenDbDeliverySet> venDbDeliverySets = this.saveOrUpdateCar(saveDatas);
            if (!StringUtil.IsEmptyOrNull(venDbDeliverySets)) {
                int addDelivery = venDbDeliverySetRepositories.venDbDeliverySetMapperAddVenDbCarDeliveryDaySetSave(venDbDeliverySets);
//                int addDelivery = venDbDeliverySetMapper.addVenDbCarDeliveryDaySetSave(venDbDeliverySets);
                if (addDelivery == 0) {
                    throw BizException.newBizException("500", "500");
                }

            }
            resutl = "批量保存预交车天数设置成功！";

        }
        /**
         * 若modifyType为allSame时的全部同步，先根据查询条件调用《预交车天数设置-查询》方法，得到和表格一样的数据，
         * 循环此数据；在循环过程中，先以专营店id查询表【t_oms_ven_db_delivery_set】中是否存在数据，
         * 若存在数据，则根据saveDatas中的基线数据【preDeliveryDays、deliveryNum】更新当前数据；
         * 若不存在数据则根据saveDatas中的基线数据【preDeliveryDays、deliveryNum】则新增数据
         */
        if ("allSame".equals(deliverySetSaveDomainIn.getModifyType())) {
            DeliverySetDomainIn deliverySetDomainIn = new DeliverySetDomainIn();
            BeanUtil.copyProperties(deliverySetSaveDomainIn, deliverySetDomainIn);
            deliverySetDomainIn.setOemId(BusinessContext.getBusinessContext().getOemId());
            deliverySetDomainIn.setGroupId(BusinessContext.getBusinessContext().getGroupId());
            List<DeliverySetDomainOut> deliverySetDomainOutList = venDbDeliverySetRepositories.venDbDeliverySetMapperVenDbCarDeliveryDaySetQuery(null,deliverySetDomainIn);
//            List<DeliverySetDomainOut> deliverySetDomainOutList = venDbDeliverySetMapper.venDbCarDeliveryDaySetQuery(null,deliverySetDomainIn);
            List<VenDbDeliverySet> venDbDeliverySetList = new ArrayList<>();
            if (!StringUtil.IsEmptyOrNull(deliverySetDomainOutList)) {
                for (DeliverySetDomainOut deliverySetDomainOut : deliverySetDomainOutList) {
                    VenDbDeliverySet venDbDeliverySet = new VenDbDeliverySet();
                    BeanUtil.copyProperties(deliverySetDomainOut, venDbDeliverySet);
                    venDbDeliverySet.setPreDeliveryDays(saveDatas.get(0).getPreDeliveryDays());
                    venDbDeliverySet.setDeliveryDays(saveDatas.get(0).getDeliveryDays());
                    venDbDeliverySet.setCarBrandCode(saveDatas.get(0).getCarBrandCode());
                    venDbDeliverySet.setCarBrandCn(saveDatas.get(0).getCarBrandCn());
                    venDbDeliverySet.setIsEnable(saveDatas.get(0).getIsEnable());
                    venDbDeliverySetList.add(venDbDeliverySet);
                }
                List<VenDbDeliverySet> venDbDeliverySets = this.saveOrUpdateCar(venDbDeliverySetList);
                if (!StringUtil.IsEmptyOrNull(venDbDeliverySets)) {
                    int addDelivery = venDbDeliverySetRepositories.venDbDeliverySetMapperAddVenDbCarDeliveryDaySetSave(venDbDeliverySets);
//                    int addDelivery = venDbDeliverySetMapper.addVenDbCarDeliveryDaySetSave(venDbDeliverySets);
                    if (addDelivery == 0) {
                        throw BizException.newBizException("500", "500");
                    }

                }

                resutl = "全量保存预交车天数设置成功！";

            } else {
                throw BizException.newBizException("500", "查询数据为空");
            }

        }

        return LyOptResultUtil.success(resutl);
    }

    public ListResult<DeliverySetDomainOut> venDbCarDeliveryDaySetQuery(DeliverySetDomainIn deliverySetDomainIn){
        ListResult<DeliverySetDomainOut> result = venDbDeliverySetRepositories.venDbCarDeliveryDaySetQuery(deliverySetDomainIn);
        return result;
    }

    public OptResult venDbCarDeliveryDaySetQueryCount(DeliverySetDomainIn deliverySetDomainIn){
        OptResult optResult = venDbDeliverySetRepositories.venDbCarDeliveryDaySetQueryCount(deliverySetDomainIn);
        return optResult;
    }

    public  List<VenDbDeliverySet> saveOrUpdateCar(List<VenDbDeliverySet> saveDatas) {
        List<VenDbDeliverySet> adddeliverySetListVenDb = new ArrayList<>();
        List<String> dlrIds = saveDatas.stream().map(VenDbDeliverySet::getDlrId).collect(Collectors.toList());
        QueryWrapper<VenDbDeliverySet> deliverySetEntityQueryWrapper = new QueryWrapper<>();
        deliverySetEntityQueryWrapper.lambda().in(VenDbDeliverySet::getDlrId, dlrIds);
        List<VenDbDeliverySet> venDbDeliverySets = venDbDeliverySetRepositories.venDbDeliverySetMapperSelectList(deliverySetEntityQueryWrapper);
        List<String> carCroutSetIds = venDbDeliverySets.stream().map(VenDbDeliverySet::getCaroutSetId).collect(Collectors.toList());
        if (!StringUtil.IsEmptyOrNull(carCroutSetIds)) {
            int i = venDbDeliverySetRepositories.venDbDeliverySetMapperDeleteDeliverSetBatch(carCroutSetIds);
            if (i != carCroutSetIds.size()) {
                throw BizException.newBizException("500", "500");
            }
        }
        for (VenDbDeliverySet venDbDeliverySet : saveDatas) {
            venDbDeliverySet.setCaroutSetId(LyIdUtils.genId());
            StringUtil.initUserInfo(venDbDeliverySet, StringUtil.SaveOrUpdate.Save);
            adddeliverySetListVenDb.add(venDbDeliverySet);
        }
        return adddeliverySetListVenDb;
    }

}